<template>
	<view class="view">
		<view class="nav-bar" :style="[{ paddingTop: StatusBar + 'px' }]">
			<view class="nav-bar-item" :style="[{ height: CustomBar ? CustomBar + 'px' : 'auto', width: StatusWidth ? StatusWidth + 'px' : '100%' }]">
				<uni-icons @click="goBack" type="arrowleft" color="white" size="25"></uni-icons>
				<!-- <uni-icons @click="favorite(details.followStatus == 0 ? 0 : 1)"
					:type="details.followStatus == 0 ? 'star':'star-filled'"
					:color="details.followStatus == 0 ? 'white':'red'" size="25"></uni-icons> -->
			</view>
		</view>

		<view class="head" :style="[{ paddingTop: StatusBar + CustomBar + 'rpx' }]">
			<view class="head-item">
				<view class="title">
					<text>{{ details.doctorName }}</text>
					<image v-if="details.sex == 2" src="/static/images/female.png" />
					<image v-if="details.sex == 1" src="/static/images/male.png" />
					<text>{{ details.departmentName }}</text>
					<text>{{ details.doctorPosition }}</text>
					<view class="btn" :class="details.followStatus == 1 ? 'active' : ''" @click="favorite(details.followStatus == 0 ? 0 : 1)">
						{{ details.followStatus == 1 ? '已关注' : '关注' }}
					</view>
				</view>
				<navigator style="color: #55aaff;" :url="`/pages/IndexPages/hospitalDetails/main?id=${details.hospitalId}`" class="wrap">
					{{ details.hospitalName || '' }}
				</navigator>
				<view class="evaluation">
					<text>{{ details.positiveRate || 0 }}%</text>
					<text>好评率</text>
					<text>{{ details.reviewsNumber || 0 }}</text>
					<text>评价数</text>
				</view>
				<view class="good-at">{{ details.specialty }}</view>
			</view>
		</view>

		<view class="evaBox">
			<view class="title">诊疗评价</view>
			<view class="tags-name">医生印象</view>
			<view class="taglist">
				<view class="tags" v-for="(item, index) in tagList" :key="index" @click="select(index, 0)" :class="index == selectIndex ? 'select' : ''">
					{{ item.tagName }}({{ item.evaluateNumber }})
				</view>
			</view>
			<view class="tags-name">疾病投票</view>
			<view class="taglist">
				<view class="tags" v-for="(item, index) in diseasesList" :key="index" @click="select(index, 1)" :class="index == diseaseIndex ? 'select' : ''">
					{{ item.diseaseName }}({{ item.evaluateNumber }})
				</view>
			</view>
			<!-- 评论列表 -->
			<view class="item" v-for="(item, index) in list" :key="index">
				<view class="headerimg">
					<view class="head-left">
						<image :src="item.userAvatar || defaultAvatar" mode="aspectFill" />
						<view class="name">
							<view>{{ item | formatName }}</view>
							<view>{{ item.diseaseName || '' }}</view>
						</view>
					</view>
					<view class="head-right">{{ item.createTime }}</view>
				</view>
				<view class="tag taglist">
					<view>医生印象：</view>
					<view v-for="(val, k) in item.commentTagsList" :key="k">{{ k > 0 ? '，' : '' }} {{ val.tagName }}</view>
				</view>
				<view class="detail">
					<view class="text">{{ item.additionalRemark }}</view>
				</view>
			</view>
		</view>
		<view class="Btn"><button @click="to()">就诊体验</button></view>
	</view>
</template>

<script>
import { getDoctorDetails, selectListByDoctorId, addFocus, cancelFocus, addBrowserHistory } from '../../../apis/request.js';

export default {
	data() {
		return {
			StatusBar: 0,
			CustomBar: 0,
			StatusWidth: 0,

			list: [],
			details: [],
			userId: '1',
			doctorId: '',

			selectIndex: 0,
			diseaseIndex: 0,
			pageNo: 1,
			//标签列表
			tagList: [
				{
					tagId: '',
					tagName: '全部',
					evaluateNumber: 0
				}
			],
			//疾病列表
			diseasesList: [
				{
					diseaseId: '',
					diseaseName: '全部',
					evaluateNumber: 0
				}
			],
			finished: false,
			//关注状态
			defaultAvatar: require('../../../static/images/default_avatar.png')
		};
	},
	filters: {
		formatName(item) {
			if (item.userName) {
				if (item.isAnonymous == 1) {
					return item.userName.substring(0, 1) + item.userName.substring(1, item.userName.length).replace(/./g, '*');
				} else {
					return item.userName;
				}
			} else {
				return '***';
			}
		}
	},
	methods: {
		goBack() {
			uni.navigateBack();
		},
		favorite(status) {
			// status = 0 说明没有关注 那就关注; = 1 说明关注了 取消关注
			if (status == 0) {
				addFocus({
					attentionId: this.doctorId,
					attentionType: 1
				}).then(() => {
					this.details.followStatus = 1;
				});
			} else {
				this.$dialog('提示', '是否取消关注').then(() => {
					cancelFocus({
						attentionId: this.doctorId,
						attentionType: 1
					}).then(() => {
						this.details.followStatus = 0;
					});
				});
			}
		},
		Imgpreview(cur, arrIndex) {
			this.$Imgpreview(cur, this.list[arrIndex].imgList);
		},
		//筛选标签
		select(index, type) {
			this.list = [];
			this.pageNo = 1;
			this.finished = false;
			//标签
			if (type == 0) {
				this.selectIndex = index;
				this.tagId = this.tagList[index].tagId;
			}
			//疾病
			if (type == 1) {
				this.diseaseIndex = index;
				this.diseaseId = this.diseasesList[index].diseaseId;
			}
			this.getlist();
		},
		//获取标签数据
		async getlist() {
			// 获取医生评论列表
			if (!this.finished) {
				uni.stopPullDownRefresh();
				const data = await selectListByDoctorId({
					doctorId: this.doctorId,
					pageNo: this.pageNo++,
					pageSize: 10,
					//标签id
					tagId: this.tagId,
					//疾病id
					diseaseId: this.diseaseId
				});
				if (!this.tagId && !this.diseaseId) {
					this.tagList[0].evaluateNumber = data.total;
					this.diseasesList[0].evaluateNumber = data.total;
				}
				this.list.push(
					...data.records.map(item => {
						return {
							...item,
							imgList: item.commentImage ? item.commentImage.split(',') : item.commentImage
						};
					})
				);
				//如果数组中的值为空 就不加载
				if (data.records.length == 0) {
					this.finished = true;
				}
			}
		},
		to() {
			const token = uni.getStorageSync('token');
			if (token) {
				uni.navigateTo({
					url: '/pages/IndexPages/medical/main?id=' + this.doctorId
				});
			} else {
				uni.navigateTo({
					url: '/pages/LoginPages/mylogin/main'
				});
			}
		},
		//获取详情列表
		async getdetails() {
			this.tagList = [
				{
					tagId: '',
					tagName: '全部',
					evaluateNumber: 0
				}
			];
			this.diseasesList = [
				{
					diseaseId: '',
					diseaseName: '全部',
					evaluateNumber: 0
				}
			];
			this.details = await getDoctorDetails({
				doctorId: this.doctorId
			});
			//获取标签列表
			this.tagList.push(
				...this.details.evaluateTagsDtoList.map(item => {
					return {
						...item
					};
				})
			);
			//获取疾病列表
			this.diseasesList.push(
				...this.details.evaluateInfoDtoList.map(item => {
					return {
						...item
					};
				})
			);
		},
		addRecord() {
			addBrowserHistory({
				type: 0,
				objectId: this.doctorId
			}).then(res => {});
		}
	},
	onLoad(options) {
		let that = this;
		this.tagId = this.tagList[0].tagId;
		this.diseaseId = this.diseasesList[0].diseaseId;
		this.doctorId = options.id;
		this.getdetails();
		this.getlist();
		if (uni.getStorageSync('token')) {
			//登录下添加浏览记录
			this.addRecord();
		}
		//计算头部高度
		//#ifdef MP-WEIXIN
		const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
		uni.getSystemInfo({
			success(res) {
				that.StatusWidth = res.screenWidth - menuButtonInfo.width;
				that.StatusBar = res.statusBarHeight;
				that.CustomBar = menuButtonInfo.bottom + menuButtonInfo.top - res.statusBarHeight;
			}
		});
		//#endif
	},
	onShow() {
		uni.$on('commentRefresh', () => {
			this.pageNo = 1;
			this.list = [];
			this.getdetails();
			this.getlist();
		});
	},
	onReachBottom() {
		this.getlist();
	},
	onPullDownRefresh() {
		this.list = [];
		this.finished = false;
		this.pageNo = 1;
		this.getlist();
	},
	onShareAppMessage() {
		const title = this.details.doctorName + '-' + this.details.departmentName + '-' + this.details.hospitalName;
		const id = this.doctorId;
		return {
			title: title,
			imageUrl: this.$shareImageUrl,
			path: 'pages/IndexPages/doctorDetail/main?id=' + id
		};
	}
};
</script>

<style lang="scss" scoped>
.view {
	height: auto;
	padding-bottom: 180rpx;
	padding-top: 120rpx;

	.status_bar {
		height: var(--status-bar-height);
	}

	.nav-bar {
		position: fixed;
		top: 0;
		left: 0;
		z-index: 2;
		background: linear-gradient(70deg, #32C59A, #32C59A);
		height: 100rpx;
		width: 100vw;

		.nav-bar-item {
			justify-content: space-between;
			box-sizing: border-box;
			padding: 20rpx;
			display: flex;
		}
	}

	.head {
		top: -29rpx;
		position: relative;
		background: linear-gradient(70deg, #32C59A, #32C59A);

		.head-item {
			background-color: #ffffff;
			padding: 30rpx;
			border-bottom: 1rpx solid #e8e8e8;
			position: relative;
			border-radius: 40rpx 40rpx 0 0;

			.iconBox {
				top: 30rpx;
				right: 30rpx;
				position: absolute;

				image {
					width: 59rpx;
					height: 53rpx;
				}
			}

			view {
				display: flex;
				align-items: center;
			}

			.title {
				.btn {
					margin-left: 20upx;
					width: 90upx;
					height: 34upx;
					display: flex;
					flex-direction: row;
					align-items: center;
					justify-content: center;
					font-size: 22upx;
					color: #dd1d1a;
					border: 1upx solid #dd1d1a;
					border-radius: 18upx;

					&.active {
						color: #999999;
						border: 1upx solid #dcdcdc;
					}
				}

				text {
					&:first-child {
						font-size: 36rpx;
					}

					&:nth-child(n + 2) {
						font-size: 30rpx;
						margin-left: 10rpx;
					}
				}

				image {
					width: 36rpx;
					height: 36rpx;
					margin: 0 10rpx;
				}
			}

			.wrap {
				margin-top: 20rpx;
				font-size: 30rpx;
			}

			.evaluation {
				margin-top: 20rpx;

				text {
					&:nth-child(odd) {
						font-size: 30rpx;
						color: #f0ad4e;
					}

					&:nth-child(n + 2) {
						margin-left: 10rpx;
					}
				}
			}

			.good-at {
				margin-top: 30rpx;
				color: #939393;
				font-size: 28rpx;
			}
		}
	}

	.evaBox {
		background-color: #ffffff;
		padding: 40rpx 30rpx 0;

		.title {
			font-size: 32rpx;
			margin-bottom: 20rpx;
		}

		.tags-name {
			margin-top: 10rpx;
			font-size: 28rpx;
			color: #999999;
		}

		.taglist {
			display: flex;
			flex-wrap: wrap;
			margin-top: 15rpx;

			.tags {
				height: 58rpx;
				padding: 0rpx 15rpx;
				background: #f1efef;
				border-radius: 29rpx;
				font-size: 24rpx;
				color: #333333;
				line-height: 58rpx;
				margin: 10rpx 20rpx 10rpx 0rpx;
			}

			.select {
				background: #32C59A;
				color: #ffffff;
			}
		}

		.item {
			margin-top: 30rpx;
			border-bottom: 1rpx solid #f1f1f1;

			.headerimg {
				display: flex;
				justify-content: space-between;

				.head-left {
					display: flex;
					align-items: flex-end;
					justify-content: center;

					image {
						border-radius: 50%;
						width: 80rpx;
						height: 80rpx;
						margin-right: 20rpx;
					}

					.name {
						view:nth-child(1) {
							font-size: 28rpx;
							color: #020202;
						}

						view:nth-child(2) {
							font-size: 22rpx;
							color: #f0ad4e;
						}
					}
				}

				.head-right {
					font-size: 20rpx;
					color: #999999;
				}
			}

			.tag {
				font-size: 24rpx;
				color: #999999;
				margin: 15rpx 0;
			}

			.detail {
				padding-bottom: 30rpx;

				.text {
					word-break: break-all;
					font-size: 28rpx;
					color: #333333;
				}

				.imgBox {
					image {
						width: 220rpx;
						height: 250rpx;
						margin-right: 8rpx;
					}
				}
			}
		}
	}

	.Btn {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		display: flex;
		justify-content: center;
		padding: 30rpx 0;
		background-color: #ffffff;

		button {
			width: 690rpx;
			height: 88rpx;
			background: #32C59A;
			border-radius: 44px;
			font-size: 30rpx;
			color: #ffffff;
			text-align: center;
			line-height: 88rpx;
		}
	}
}
</style>
